import pandas as pd
import os
import numpy as np

folder_path = 'D:\\accessability\\ACCESSIBILITY_RESULTS\\Model_Experiment_Result_Raw_Data'

# List of model names and languages
themes = [
    "GPT4.1_CHINESE", "GPT4.1_ENGLISH", "Gemini_1.5_Pro_CHINESE", "Gemini_1.5_Pro_ENGLISH",
    "GPT4O_CHINESE", "GPT4O_ENGLISH", "CLAUDE_CHINESE", "CLAUDE_ENGLISH",
    "LLAMA4_CHINESE", "LLAMA4_ENGLISH", "QWEN_CHINESE", "QWEN_ENGLISH"
]

# Define three sets of intervals based on viewpoint conditions:
# - First-person viewpoint: [(0, 20), (60, 80), (120, 140)]
# - Third-person viewpoint (same angle): [(20, 40), (80, 100), (140, 160)]
# - Third-person viewpoint (different angle): [(40, 60), (100, 120), (160, 180)]

interval_sets = [[(0, 20), (60, 80), (120, 140)]]    #[(20, 40), (80, 100), (140, 160)] #[(40, 60), (100, 120), (160, 180)]

# Store statistics
statistics = []

# Process each theme
for theme in themes:
    set_accuracies = []

    # Ten runs per theme
    for run in range(1, 11):
        file_name = f"{theme}_for_questions_answers_run_{run}.csv"
        file_path = os.path.join(folder_path, file_name)

        if os.path.exists(file_path):
            df = pd.read_csv(file_path)

            # Compute accuracy for each interval set
            for interval_set in interval_sets:
                correct_count = 0
                total_count = 0

                for start, end in interval_set:
                    subset = df.iloc[start:end]
                    correct_count += (subset['Comparison'] == 1).sum()
                    total_count += len(subset)

                if total_count > 0:
                    set_accuracies.append(correct_count / total_count)

    # Aggregate statistics
    if set_accuracies:
        average = np.mean(set_accuracies)
        std_dev = np.std(set_accuracies)
        max_value = np.max(set_accuracies)
        min_value = np.min(set_accuracies)

        statistics.append({
            "Theme": theme,
            "Average ± Std Dev": f"{average:.4f} ± {std_dev:.4f}",
            "Max": round(max_value, 4),
            "Min": round(min_value, 4)
        })

# Output results
output_df = pd.DataFrame(statistics)
output_path = "D:\\accessability\\ACCESSIBILITY_RESULTS\\Model_Experiment_Result_Statistics_Data\\Analysis of three viewpoints"
output_file_path = os.path.join(output_path, 'First_person_accuracy_statistics.csv')
output_df.to_csv(output_file_path, index=False)

print(f"All statistical data has been computed and saved to the file: {os.path.basename(output_file_path)}")
